//
//  LGTInputTextView.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/22.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTInputTextView: UIView, UITextViewDelegate {
    let placehoderLabel:UILabel = UILabel.init()
    let textView:UITextView = UITextView.init()
    let countLabel:UILabel = UILabel.init()
    var text:String?{
        get{
            return self.textView.text
        }set{
            self.textView.text = newValue
            self.placehoderLabel.isHidden = newValue?.count ?? 0 > 0 ? true:false
        }
    }
    
    private var _showCount:Bool = false
    var returnReturnStrBlock:ClickReturnStrBlock?
    var showCount:Bool{
        get{
            return _showCount
        }set{
            _showCount = newValue
            self.countLabel.text = "\(self.textView.text.count)/\(self.limitCount)"
            self.countLabel.isHidden = !newValue
        }
    }
    
    var limitCount:Int = -1
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(textView)
        self.addSubview(self.placehoderLabel)
        
        self.textView.isScrollEnabled = false
        self.textView.textContainerInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        self.placehoderLabel.textAlignment = NSTextAlignment.left
        self.placehoderLabel.textColor = ColorUtil(color: 0x999999)
        self.placehoderLabel.numberOfLines = 0
        self.placehoderLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(5))
            make.right.equalTo(SizeFit(-5))
            //make.height.equalTo(SizeFit(20))
            make.top.equalTo(SizeFit(0))
        }
        
        self.textView.delegate = self
        self.textView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalTo(self)
        }
        
        self.countLabel.textColor = GrayColor
        self.countLabel.font = Font_Normal_12
        self.addSubview(self.countLabel)
        self.countLabel.text = ""
        //self.countLabel.isHidden = true
        self.countLabel.snp.makeConstraints { (make) in
            make.right.equalTo(-SizeFit(5))
            make.height.equalTo(SizeFit(12))
            make.top.equalTo(self.snp_bottomMargin).offset(10)
        }
        
       // self.textView.keyboardType = UIKeyboardType.numberPad
        
        //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardChange), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    //键盘即将收起
    @objc func keyBoardChange(note:Notification){
        if self.textView.isFirstResponder == true{
            print("yes")
            //值发生变化 block回掉
            if self.returnReturnStrBlock != nil{
                self.returnReturnStrBlock!(textView.text)
            }
        }else{
            //print("salse")
        }
    }
    
    deinit{
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    func updateInfo(fontSize:CGFloat, textColor:UIColor, limitCount:Int, placeHoder:String=""){
        self.textView.font = KFont(size: fontSize)
        self.placehoderLabel.font = KFont(size: fontSize)
        self.placehoderLabel.text = placeHoder
        self.textView.textColor = textColor
        self.limitCount = limitCount
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func becomeFirstResponder() -> Bool {
        super.becomeFirstResponder()
        self.textView.becomeFirstResponder()
        return true
    }
    

    func textViewDidChange(_ textView: UITextView) {
        
        self.placehoderLabel.isHidden = textView.text.count > 0 ? true:false
        
        let text = textView.textInputMode?.primaryLanguage
        if text?.elementsEqual("zh-Hans") ?? false{
            let selectedRange = textView.markedTextRange
            if selectedRange == nil{
                self.setTextStyle(textView: textView)
            }else{
                //获取高亮部分
            }
        
        }else{
            self.setTextStyle(textView: textView)
        }
    }
    
    func setTextStyle(textView:UITextView){
        let count = textView.text.count
        if count > self.limitCount && self.limitCount != -1{
            var text:String = textView.text
            text = String(text.prefix(self.limitCount))
//
//            let offsetIndex = text.index(text.startIndex, offsetBy: self.limitCount)
//            text = String(text[..<offsetIndex])
            textView.text = text
        }
        
        self.countLabel.text = "\(textView.text.count)/\(self.limitCount)"
        self.countLabel.isHidden = !self.showCount
        
//        //值发生变化 block回掉
//        if self.returnReturnStrBlock != nil{
//            self.returnReturnStrBlock!(textView.text)
//        }
    }
    
    
//    - (void)setTextStyle:(UITextView *)textView{
//    NSInteger length = textView.text.length;
//    if(length > self.limitCount && self.limitCount != 0){
//    NSString *text = [textView.text substringWithRange:NSMakeRange(0, self.limitCount)];
//    length = self.limitCount;
//    textView.text = text;
//    }
//
//    self.limitConntLabel.text = [NSString stringWithFormat:@"%ld/%ld", length, self.limitCount];
//
//
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    paragraphStyle.lineSpacing = 5;// 字体的行间距
//    NSDictionary *attributes = @{
//
//    NSFontAttributeName:self.textView.font,
//    NSParagraphStyleAttributeName:paragraphStyle
//
//    };
//    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
//
//    //值发生变化
//    if(self.valueChangeBlock){
//    self.valueChangeBlock(textView.text);
//    }
//    }
    
    
    
}
